# coding:utf-8
# @Time : 2021/5/7 12:50 
# @Author : minuy
# @File : auth.py
from flask import request

from app import login_auth
from app.api import api
from app.api.interface import api_result
from app.models.user import User


@api.route('/login', methods=['POST'])
def get_auth():
    # username = request.args.get('username')
    # print(username)
    # password = request.args.get('password')
    # print(password)
    # Vue请求的数据在request.get_json()里面
    if request.get_json():
        username = request.get_json().get('username')
        print(username)
        password = request.get_json().get('password')
        print(password)
        print(request.get_json())
        if username and password:
            user = User.query.filter_by(username=username).first()
            if user and user.check_password(password):
                print('登录成功！', user.username)
                token = login_auth.get_token(username, user.password)
                return api_result(code=200, data={'token': token}, message='登录成功')
            else:
                print('登录失败！')
                return api_result(code=403, message='账号或密码错误')
    return api_result(code=403, message='参数不完整')

